from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings


def dumps(json, expires):
    # 将参数字典  加密  返回字符串
    # 1创建工具对象
    serializer = Serializer(settings.SECRET_KEY, expires)
    # 2加密
    s1 = serializer.dumps(json)
    # 3转字符串  返回
    return s1.decode()


def loadds(s1, expires):
    # 将参数字符串  解密为  字典
    # 1创建工具对象
    # serializer = Serializer(settings.SECRET_KEY, expires)
    # # 2解密
    # json = serializer.loads(s1)
    # # 3返回none
    # return json

    # 如果字符串被改过，或者过期就解密不了
    # 将参数字符串  解密为  字典
    # 1创建工具对象
    serializer = Serializer(settings.SECRET_KEY, expires)
    # 2解密
    try:
        json = serializer.loads(s1)
    # 3返回none
    except:
        return None
    return json
